package de.hpi.generator.model;

import de.hpi.generator.generator.storages.ValueSeriaInfo;
import java.io.Serializable;
import java.util.ArrayList;

import de.hpi.generator.config.NotExistsException;

public class ColumnsCollection extends ArrayList<Column> implements Serializable {

    private Table aOwner = null;

    public ColumnsCollection(Table aOwner) {
        this.aOwner = aOwner;
    }
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Column findColumnByName(String aColumnName) throws NotExistsException {
        Column result = null;
        for (Column aColumn : this) {
            if (aColumn.getName().compareTo(aColumnName) == 0) {
                result = aColumn;
                break;
            }
        }
        if (result == null) {
            throw new NotExistsException("The column " + aColumnName + " was not found in table " + aOwner.getName());
        }

        return result;
    }

    public Table getOwner() {
        return aOwner;
    }

    public int getMaxNumberOfUniqueValues() {
        int result = 0;

        for (Column aColumn : this) {

            int aNumberOfValues = aColumn.getNumberOfUniqueValues();
            if (aNumberOfValues > result) {
                result = aNumberOfValues;
            }
        }

        return result;
    }

    public int getMaxNumberOfValues() {
        int result = 0;

        for (Column aColumn : this) {
            int aNumberOfValues = aColumn.getNumberOfValues();
            if (aNumberOfValues > result) {
                result = aNumberOfValues;
            }
        }

        return result;
    }

    public boolean isOpen() throws Exception {
        for (Column aColumn : this) {
            if (aColumn.isOpen()) {
                return true;
            }
        }
        return false;
    }

    public void openForRead() throws Exception {
        for (Column aColumn : this) {
            aColumn.openForRead();
        }
    }

    public void closeAfterRead() throws Exception {
        for (Column aColumn : this) {
            aColumn.closeAfterRead();
        }
    }

    public void openForAppend() throws Exception {
        for (Column aColumn : this) {
            aColumn.openForAppend();
        }
    }

    public void closeAfterAppend() throws Exception {
        for (Column aColumn : this) {
            aColumn.closeAfterAppend();
        }
    }

    public ArrayList<ValueSeriaInfo> readNextValueSeriaInfo() throws Exception {
        ArrayList<ValueSeriaInfo> result = new ArrayList<ValueSeriaInfo>();

        for (Column aColumn : this) {
            result.add(aColumn.readNextValueSeriaInfo());
        }

        return result;
    }

    public void markAsGenerated() {
        for (Column aColumn : this) {
            aColumn.markAsGenerated();
        }
    }
}
